namespace ICommonLibrary.Date {

    public class Frequency {

        public static readonly Frequency OnceOff = new Frequency("Once Off");
	    public static readonly Frequency Weekly = new Frequency("Weekly");
	    public static readonly Frequency Fortnightly = new Frequency("Fortnightly");
	    public static readonly Frequency Monthly = new Frequency("Monthly");
	    public static readonly Frequency Yearly = new Frequency("Yearly");

        private static readonly Frequency[] Values = { OnceOff, Weekly, Fortnightly, Monthly, Yearly };
	    public string Name { get; private set;}

	    private Frequency(string name) {
            Name = name;
	    }
    	
	    public override string ToString() {
            return Name;
	    }
    	
	    /**
	     * Performs best effort matching up. For example
	     * <pre>
	     * 		Frequency.getInstance("week");   		// return Frequency.WEEKLY
	     * 		Frequency.getInstance("weekly");		// return Frequency.WEEKLY
	     * 		Frequency.getInstance("weeklyPayment");	// return Frequency.WEEKLY
	     * </pre>
	     */
	    public static Frequency GetInstance(string name) {
		    if (name == null)
			    return null;
    		
		    foreach (Frequency freq in Values) {
			    if (freq.Name.Equals(name))
				    return freq;
		    }
    		
		    name = name.ToLower();
    		
		    if (name.IndexOf("once") != -1)
			    return OnceOff;

            if (name.IndexOf("week") != -1)
			    return Weekly;

            if (name.IndexOf("fortnight") != -1)
			    return Fortnightly;

            if (name.IndexOf("month") != -1)
			    return Monthly;

            if (name.IndexOf("year") != -1 ||
                name.IndexOf("annual") != -1)
			    return Yearly;

		    return null;
	    }
        
    }
}